//
//  KeHuChaChongViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/4.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "KeHuChaChongViewController.h"
#import "KeHuListTableViewCell.h"
#import "KeHuLeiXingShaiXuanView.h"
#import "KeHuXiangQingViewController.h"
#import "XinJianKeHuViewController.h"
@interface KeHuChaChongViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate,UITextFieldDelegate>{
    KeHuLeiXingShaiXuanView *leiXingShaiXuanView;
    NSString *sceneId;
    NSArray *dataSource;
}
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *mainTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITextField *searchText;
@property (weak, nonatomic) IBOutlet UIButton *leiXingButton;

@property (weak, nonatomic) IBOutlet UIButton *keHuShaiXuanButton;
@end

@implementation KeHuChaChongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.customNavBar.title = @"查重";
    
    _topSpace.constant = NavaBarHeight;
    [_mainTableView registerNib:[UINib nibWithNibName:@"KeHuListTableViewCell" bundle:nil] forCellReuseIdentifier:@"KeHuListTableViewCell"];
    
    sceneId = @"0";
    leiXingShaiXuanView = [KeHuLeiXingShaiXuanView view];
    leiXingShaiXuanView.type = 2;
    leiXingShaiXuanView.dataSource = @[@{@"name":@"客户名称",@"sceneId":@"0"},@{@"name":@"公司名称",@"sceneId":@"1"}];
    leiXingShaiXuanView.manageClickBlock = ^(NSString * _Nonnull result, NSString * _Nonnull resultName) {
        _keHuShaiXuanButton.selected = NO;
        leiXingShaiXuanView.hidden = YES;
        _leiXingButton.selected = NO;
        if(resultName.length > 0){
            [_leiXingButton setTitle:resultName forState:UIControlStateNormal];
            
            sceneId = result;
            
            [self anZhaoXingMingChaChong];
        }
       
    };
    
    [self.view addSubview:leiXingShaiXuanView];
    leiXingShaiXuanView.sd_layout
    .leftEqualToView(_mainTableView)
    .rightEqualToView(_mainTableView)
    .topEqualToView(_mainTableView)
    .bottomEqualToView(_mainTableView);
    leiXingShaiXuanView.hidden = YES;
    
    [self anZhaoXingMingChaChong];
    
    
    // 创建一个手势识别器
       UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
                                              initWithTarget:self
                                              action:@selector(dismissKeyboard:)];
       
       // 设置手势识别器不取消视图中的触摸
       tapGesture.cancelsTouchesInView = NO;
//       tapGesture.delaysTouchesBegan = YES;
       // 将手势识别器添加到view
       [self.view addGestureRecognizer:tapGesture];
}

- (void)dismissKeyboard:(UITapGestureRecognizer *)recognizer {
    CGPoint location = [recognizer locationInView:self.mainTableView];
    NSIndexPath *indexPath = [self.mainTableView indexPathForRowAtPoint:location];

    if (!indexPath) {
        // 点击的不是UITableViewCell，收起键盘
        [self.view endEditing:YES];
    }
}

//姓名查重
- (void)anZhaoXingMingChaChong{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    if([sceneId integerValue] == 0){
        [paraDic setValue:_searchText.text forKey:@"name"];
    }else{
        [paraDic setValue:_searchText.text forKey:@"company"];
    }
    
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/crmCustomer/dataCheck" andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            dataSource = [info objectForKey:@"data"];
            [_mainTableView reloadData];
        }
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    [self anZhaoXingMingChaChong];
    return YES;
}

//筛选
- (IBAction)shaiXuan:(UIButton *)sender {
    [self.view endEditing:YES];
    sender.selected = !sender.selected;
    leiXingShaiXuanView.hidden = !sender.selected;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 260;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    KeHuListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KeHuListTableViewCell"];
    NSDictionary *dataDic = dataSource[indexPath.row];
    cell.nameLabel.text = GetHandleString(dataDic, @"name");
//    cell.phoneLabel.text = GetHandleString(dataDic, @"mobile");
    cell.phoneLabel.hidden = YES;
    cell.fuZeRenLabel.text = GetHandleString(dataDic, @"ownerUserName");
    cell.genJinShiJianLabel.text = GetHandleString(dataDic, @"lastTime");
    cell.juLiGongHaiLabel.text = [NSString stringWithFormat:@"%ld天",[GetHandleString(dataDic, @"poolDay") integerValue]];
//    cell.zhuangTaiLabel.text = GetHandleString(dataDic, @"level");
    cell.zhuangTaiView.hidden = YES;
//    cell.jinRuGongHaiView.hidden = YES;
    cell.jinRuRemindLabel.text = @"公司名称";
    cell.juLiGongHaiLabel.text = GetHandleString(dataDic, @"company");
    cell.leftButton.hidden = YES;
    cell.rightButton.hidden = YES;
    cell.centenrButton.hidden = NO;
//    cell.gongHaiViewHeight.constant = 0;
    cell.manageClickBlock = ^(NSInteger index) {
        if(index == 102){
            XinJianKeHuViewController *gongHai = [[XinJianKeHuViewController alloc] init];
            gongHai.customerId = GetHandleString(dataDic, @"id");
            [self.navigationController pushViewController:gongHai animated:YES];
        }
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.view endEditing:YES];
    KeHuXiangQingViewController *xiangQing = [[KeHuXiangQingViewController alloc] init];
    NSDictionary *dataDic = dataSource[indexPath.row];
    xiangQing.customerId = GetHandleString(dataDic, @"id");
    [self pushViewController:xiangQing];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    return -90;
}

-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"暂无搜索"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"暂无数据~"attributes: @{NSFontAttributeName: [UIFont systemFontOfSize:16],NSForegroundColorAttributeName: rgba(163, 168, 185, 1)}];
    return string;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
